package org.devocean.groupbuying.web.frontend;

import org.devocean.groupbuying.service.PlanService;
import org.devocean.groupbuying.service.ProductService;
import org.devocean.groupbuying.web.frontend.displaybeans.PlanBean;
import org.devocean.groupbuying.web.frontend.displaybeans.ProductBean;
import org.devocean.groupbuying.web.frontend.displaybeans.ProductPlanBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = { "/{lang:[a-z]{2}}/clouds/products" })
public class ProductController {

	@Autowired
	ProductService productService;
	
	@Autowired
	PlanService planService;
	
	@RequestMapping("{productID}")
	public String productDetail(@CookieValue("clientLanguage") String lang,
			@PathVariable Integer productID,ModelMap map){
		
		
		
		ProductBean productBean = new ProductBean(productService.getProductByProductID(productID, lang));
		map.put("productBean", productBean);
		return "productDetail";
	}
	
	@RequestMapping("{productID}/plans/{planID}")
	public String getPlan(@CookieValue("clientLanguage") String lang,@PathVariable Integer productID,@PathVariable Integer planID,ModelMap map){
		
		ProductBean productBean = new ProductBean(productService.getProductByProductID(productID, lang));
		map.put("productBean", productBean);
		ProductPlanBean planBean = new ProductPlanBean(planService.getPlanById(planID));
		map.put("planBean", planBean);
		return "productDetail";
	}
	
}
